package com.mall4j.cloud.product.controller.admin;

import com.mall4j.cloud.api.product.constant.CategoryLevel;
import com.mall4j.cloud.api.product.vo.AttrCategoryVO;
import com.mall4j.cloud.common.response.ServerResponseEntity;
import com.mall4j.cloud.common.security.AuthUserContext;
import com.mall4j.cloud.product.dto.CategoryDTO;
import com.mall4j.cloud.product.model.Category;
import com.mall4j.cloud.product.service.AttrCategoryService;
import com.mall4j.cloud.product.service.CategoryBrandService;
import com.mall4j.cloud.product.service.CategoryService;
import com.mall4j.cloud.api.product.vo.CategoryVO;
import com.mall4j.cloud.product.vo.CategoryBrandVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 分类
 *
 * @author JCM
 * @date 2023-04-22 13:09:00
 */
@RestController("adminCategoryController")
@RequestMapping("/admin/category")
@Api(tags = "admin-分类")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private CategoryBrandService categoryBrandService;

    @Autowired
    private AttrCategoryService attrCategoryService;

    @Autowired
    private MapperFacade mapperFacade;

    @GetMapping("/platform_categories")
    @ApiOperation(value = "获取平台所有分类信息",notes = "获取平台所有分类信息")
    public ServerResponseEntity<List<CategoryVO>> list(CategoryDTO categoryDTO) {
        //设置平台的商家id
        categoryDTO.setShopId(Long.parseLong("0"));
        return ServerResponseEntity.success(categoryService.list(categoryDTO));
    }

    @GetMapping
    @ApiOperation(value = "获取分类", notes = "根据categoryId获取分类")
    public ServerResponseEntity<CategoryVO> getByHotSearchId(@RequestParam Long categoryId) {
        return ServerResponseEntity.success(categoryService.getByCategoryId(categoryId));
    }

    @GetMapping("/shop_categories")
    @ApiOperation(value = "获取店铺所有的分类信息", notes = "获取店铺所有的分类信息")
    public ServerResponseEntity<List<CategoryVO>> listByShop(CategoryDTO categoryDTO){
        categoryDTO.setShopId(AuthUserContext.get().getTenantId());
        return ServerResponseEntity.success(categoryService.list(categoryDTO));
    }

    @PostMapping
    @ApiOperation(value = "保存分类", notes = "保存分类")
    public ServerResponseEntity<Void> save(@Valid @RequestBody CategoryDTO categoryDTO) {
        Category category = mapperFacade.map(categoryDTO, Category.class);
        category.setShopId(AuthUserContext.get().getTenantId());
        categoryService.save(category);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @ApiOperation(value = "更新分类", notes = "更新分类")
    public ServerResponseEntity<Void> update(@Valid @RequestBody CategoryDTO categoryDTO) {
        Category category = mapperFacade.map(categoryDTO, Category.class);
        category.setShopId(AuthUserContext.get().getTenantId());
        categoryService.update(category);
        return ServerResponseEntity.success();
    }

    @PutMapping("/category_enable_or_disable")
    @ApiOperation(value = "下架分类", notes = "下架分类")
    public ServerResponseEntity<Boolean> updateStatus(@Valid @RequestBody CategoryDTO categoryDTO) {
        CategoryVO categoryVO = categoryService.getByCategoryId(categoryDTO.getCategoryId());
        Category category = mapperFacade.map(categoryVO, Category.class);
        category.setStatus(categoryDTO.getStatus());
        categoryService.update(category);
        return ServerResponseEntity.success(true);
    }

    @DeleteMapping
    @ApiOperation(value = "删除分类", notes = "根据分类id删除分类")
    public ServerResponseEntity<Void> delete(@RequestParam Long categoryId) {
        //如果删除时该分类下有子类，则不能删除
        List<CategoryVO> childCategories = categoryService.getByParentId(categoryId);
        //如果查找出有子类，则直接返回错误信息
        if(childCategories.size() != 0){
            return ServerResponseEntity.showFailMsg("该分类下还含有子分类，不能删除");
        }

        //如果删除的是最低级的分类，则需要看它是否已与品牌和属性关联
        CategoryVO categoryVO = categoryService.getByCategoryId(categoryId);
        if(categoryVO.getLevel() == CategoryLevel.THIRD.value()){
            List<CategoryBrandVO> categoryBrands = categoryBrandService.getByCategoryId(categoryId);
            if(categoryBrands.size() != 0){
                return ServerResponseEntity.showFailMsg("该分类已与品牌绑定，不能删除");
            }
            List<AttrCategoryVO> attrCategories = attrCategoryService.getByCategoryId(categoryId);
            if(attrCategories.size() != 0){
                return ServerResponseEntity.showFailMsg("该分类已与属性绑定，不能删除");
            }
        }

        Long shopId = AuthUserContext.get().getTenantId();
        categoryService.deleteById(categoryId, shopId);
        return ServerResponseEntity.success();
    }

}
